package com.llvision.ndkdemoprocessdemo.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Xfermode;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.llvision.ndkdemoprocessdemo.R;


/**
 * TODO: document your custom view class.
 */
public class SelfTextView extends View {

    public static final String TAG = SelfTextView.class.getSimpleName();


    private TextPaint mTextPaint;
    private Paint mBmPaint;
    private Bitmap mBitmap;

    public SelfTextView(Context context) {
        super(context);
        init(null, 0);
    }

    public SelfTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public SelfTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
//        final TypedArray a = getContext().obtainStyledAttributes(
//                attrs, R.styleable.SelfTextView, defStyle, 0);
//
//        mExampleString = a.getString(
//                R.styleable.SelfTextView_exampleString);
//        mExampleColor = a.getColor(
//                R.styleable.SelfTextView_exampleColor,
//                mExampleColor);
//        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
//        // values that should fall on pixel boundaries.
//        mExampleDimension = a.getDimension(
//                R.styleable.SelfTextView_exampleDimension,
//                mExampleDimension);
//
//        if (a.hasValue(R.styleable.SelfTextView_exampleDrawable)) {
//            mExampleDrawable = a.getDrawable(
//                    R.styleable.SelfTextView_exampleDrawable);
//            mExampleDrawable.setCallback(this);
//        }
//
//        a.recycle();

        // Set up a default TextPaint object
        mTextPaint = new TextPaint();
        mTextPaint.setDither(false);
        mTextPaint.setTextSize(14 * 3);
        mTextPaint.setARGB(100, 255, 255, 0);
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yang);

        mBmPaint = new Paint();
//        Shader shader = new LinearGradient(100, 100, 500, 500, Color.YELLOW, Color.RED, Shader.TileMode.MIRROR);
        Shader shader = new SweepGradient(300, 300, Color.YELLOW,
                Color.RED);
        mBmPaint.setShader(shader);
//        mBmPaint.setStyle(Paint.Style.FILL);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG, "onMeasure:" + MeasureSpec.getSize(widthMeasureSpec) + "," + MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //draw circle
        canvas.drawCircle(800, 300, 200, mBmPaint);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(30);
        Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        setLayerType(View.LAYER_TYPE_HARDWARE, paint);
        final int save = canvas.saveLayer(null, paint);
        canvas.drawCircle(200 + mBitmap.getWidth() / 2, 400 + mBitmap.getHeight() / 2, mBitmap.getWidth() / 2 , paint);
        paint.setXfermode(xfermode);
        canvas.drawBitmap(mBitmap, 200f, 400f, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(save);

    }

}
